﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BaiTu.Common.Web.Helpers
{
    /// <summary>
    /// Html 帮助类
    /// </summary>
    public sealed class HtmlHelper : Framework.Interfaces.ISingletonDependency
    {
        private HtmlHelper() { }
        /// <summary>
        /// 生成制定数量的html空格符号
        /// </summary>
        /// <param name="spacesCount"></param>
        /// <returns></returns>
        public static string GetSpacesString(int spacesCount)
        {
            if (spacesCount <= 0)
            {
                return "";
            }
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < spacesCount; i++)
            {
                sb.Append(" &nbsp;&nbsp;");
            }
            return sb.ToString();
        }
        /// <summary>
        /// 检测是否符合email格式
        /// </summary>
        /// <param name="strEmail">要判断的email字符串</param>
        /// <returns>判断结果</returns>
        public static bool IsValidEmail(string strEmail)
        {
            strEmail.IsNullOrEmptyThrowException(string.Format("Email地址为Null或Empty"));
            return Common.Helpers.RegexHelper.IsValidEmail(strEmail);
        }
        public static bool IsValidDoEmail(string strEmail)
        {
            strEmail.IsNullOrEmptyThrowException(string.Format("Email地址为Null或Empty"));
            return Common.Helpers.RegexHelper.IsValidDoEmail(strEmail);
        }
        /// <summary>
        /// 检测是否是正确的Url
        /// </summary>
        /// <param name="strUrl">要验证的Url</param>
        /// <returns>判断结果</returns>
        public static bool IsURL(string strUrl)
        {
            strUrl.IsNullOrEmptyThrowException("要验证的Url地址为Null或Empty");
            return Common.Helpers.RegexHelper.IsURL(strUrl);
        }
        /// <summary>
        /// 获取Email地址所在的主机名称
        /// </summary>
        /// <param name="strEmail"></param>
        /// <returns></returns>
        public static string GetEmailHostName(string strEmail)
        {
            strEmail.IsNullOrEmptyThrowException("获取Email地址所在的主机名称,Email为Null或Empty");
            if (strEmail.IndexOf("@") < 0)
            {
                return "";
            }
            return strEmail.Substring(strEmail.LastIndexOf("@")).ToLower();
        }
        /// <summary>
        /// 返回URL中结尾的文件名
        /// </summary>		
        public static string GetFilename(string url)
        {
            if (url == null)
            {
                return "";
            }
            string[] strs1 = url.Split(new char[] { '/' });
            return strs1[strs1.Length - 1].Split(new char[] { '?' })[0];
        }
        /// <summary>
        /// 替换回车换行符为html换行符
        /// </summary>
        public static string ReplaceEnterToHang(string str)
        {
            string str2;

            if (str == null)
            {
                str2 = "";
            }
            else
            {
                str = str.Replace("\r\n", "<br />");
                str = str.Replace("\n", "<br />");
                str2 = str;
            }
            return str2;
        }

        /// <summary>
        /// 移除Html标记
        /// </summary>
        /// <param name="content"></param>
        /// <returns></returns>
        public static string RemoveHtml(string content)
        {
            return Common.Helpers.RegexHelper.RemoveHtml(content);
        }

        /// <summary>
        /// 过滤HTML中的不安全标签
        /// </summary>
        /// <param name="content"></param>
        /// <returns></returns>
        public static string RemoveUnsafeHtml(string content)
        {
            return Common.Helpers.RegexHelper.RemoveUnsafeHtml(content); ;
        }

        /// <summary>
        /// 从HTML中获取文本,保留br,p,img
        /// </summary>
        /// <param name="HTML"></param>
        /// <returns></returns>
        public static string GetTextFromHTML(string HTML)
        {
            return Common.Helpers.RegexHelper.GetTextFromHTML(HTML);
        }
    }
}
